package com.soil.code.generation.infrastructure;

import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.File;
import java.io.IOException;

/**
 * @author zeno
 * @date 2022/8/9 10:18
 */

public abstract class AbstractFreeMakerCodeGenerator {

	private static final String TEMPLATE_FILE_ENCODING = "UTF-8";

	protected Configuration templateConfig = new Configuration(Configuration.VERSION_2_3_31);

	protected AbstractFreeMakerCodeGenerator(String path) throws IOException{
		ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
		Resource[] resources = resourceResolver.getResources(path);

		templateConfig.setDirectoryForTemplateLoading(resources[0].getFile());
		templateConfig.setDefaultEncoding(TEMPLATE_FILE_ENCODING);
		templateConfig.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
	}

}
